package com.heer.gymnasium.module.article.views;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.heer.amap.utils.ToastUtil;
import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseActivity;
import com.heer.gymnasium.base.BaseRefreshActivity;
import com.heer.gymnasium.module.advise.beans.AdMainBean;
import com.heer.gymnasium.module.article.beans.ArticleDetailBean;
import com.heer.gymnasium.module.article.beans.EvaluateBean;
import com.heer.gymnasium.module.article.beans.StadiumBannerInfoBean;
import com.heer.gymnasium.module.login.views.LoginActivity;
import com.heer.gymnasium.module.main.views.FindFragment;
import com.heer.gymnasium.util.DialogUtils;
import com.heer.gymnasium.util.ImageUtil;
import com.heer.gymnasium.util.StringUtils;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;
import com.zhihuiyun.library.view.MyListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by zy on 2018/1/24.
 * 文章详情页
 */

public class ArticleStoreActivity extends BaseActivity {

    private final static int RES_ID_ROOT = R.layout.layout_article_store;
    private final static int LAYOUT_NAME_ID = R.id.layout_article_store_titlename_tv;
    private final static int LAYOUT_TIME_ID = R.id.layout_article_store_time_tv;
    private final static int LAYOUT_CONTENT_ID = R.id.layout_article_store_webview;


    @BindView(LAYOUT_NAME_ID)
    TextView titleTextView;
    @BindView(LAYOUT_TIME_ID)
    TextView timeTextView;
    @BindView(LAYOUT_CONTENT_ID)
    WebView webView;

    private ArticleStoreActivity $this;

    @Override
    protected void init() {
        super.init();
        $this = this;
        setContentView(RES_ID_ROOT);
    }

    @Override
    protected void initView() {
        super.initView();
        titleUtils.setTitleText("图片详情");
        webView.onResume();
        getStadiumBannerInfo();
    }

    @Override
    protected void onPause() {
        super.onPause();
        webView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webView.destroy();
    }

    public static void startActivity(Context context, String id) {
        Intent mIntent = new Intent(context, ArticleStoreActivity.class);
        mIntent.putExtra(EXTRA_ID, id);
        context.startActivity(mIntent);
    }

    private void getStadiumBannerInfo() {
        String id = getIntent().getStringExtra(EXTRA_ID);
        if (TextUtils.isEmpty(id)) {
            ToastUtil.show($this, "信息异常");
            return;
        }
        mAppHelper.apiService().getStadiumBannerInfo(id).enqueue(new BaseCallback<ApiResponse<StadiumBannerInfoBean>>() {
            @Override
            public void onSuccess(Call<ApiResponse<StadiumBannerInfoBean>> call, Response<ApiResponse<StadiumBannerInfoBean>> response) {
                StadiumBannerInfoBean bean = response.body().getData();
                if (bean != null) {
                    titleTextView.setText(bean.getTitle());
                    timeTextView.setText(StringUtils.timeStamp10ToDate(bean.getCreate_time(), StringUtils.DATE_FORMAT));
                    String css = "<style>body{padding:0 10px 0 10px;}</style>";
                    webView.loadDataWithBaseURL(null, css + bean.getText(), "text/html", "utf-8", null);
                    webView.setWebViewClient(new WebViewClient() {
                        @Override
                        public void onPageFinished(WebView view, String url) {
                            int w = View.MeasureSpec.makeMeasureSpec(0,
                                    View.MeasureSpec.UNSPECIFIED);
                            int h = View.MeasureSpec.makeMeasureSpec(0,
                                    View.MeasureSpec.UNSPECIFIED);
                            view.measure(w, h);
                            super.onPageFinished(view, url);

                        }
                    });
                }
            }
        });
    }
}
